package com.gaswell.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaswell.entity.Qba01M;
import com.gaswell.mapper.AlarmRecordMapper;
import com.gaswell.mapper.Qba01MMapper;
import com.gaswell.pojo.AlarmRecord;
import com.gaswell.service.AlarmRecordService;
import com.gaswell.service.QjService;
import com.gaswell.vo.QjVo;
import com.gaswell.vo.Result;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
@DS("mysql")
public class AlarmRecordServiceImpl extends ServiceImpl<AlarmRecordMapper, AlarmRecord> implements AlarmRecordService {
    @Autowired
    private AlarmRecordMapper alarmRecordMapper;
    @Autowired
    private QjService qjService;

    @Override
    public Result selectAll(int department) {
        LambdaQueryWrapper<AlarmRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (department == 2) {
            List<String> lists = qjService.findJhByYm("川渝");
            lambdaQueryWrapper.in(AlarmRecord::getJh, lists);
        }
        if (department == 3) {
            List<String> lists = qjService.findJhByYm("大庆");
            lambdaQueryWrapper.in(AlarmRecord::getJh, lists);
        }
        lambdaQueryWrapper.orderByDesc(AlarmRecord::getTime);
        return Result.success(alarmRecordMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public Result selectAllByPage(IPage<AlarmRecord> page, int department, int sort) {
        LambdaQueryWrapper<AlarmRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (department == 2) {
            List<String> lists = qjService.findJhByYm("川渝");
            lambdaQueryWrapper.in(AlarmRecord::getJh, lists);
        }
        if (department == 3) {
            List<String> lists = qjService.findJhByYm("大庆");
            lambdaQueryWrapper.in(AlarmRecord::getJh, lists);
        }
        if (sort == 1)
            lambdaQueryWrapper.orderByAsc(AlarmRecord::getTime);
        else
            lambdaQueryWrapper.orderByDesc(AlarmRecord::getTime);
        IPage<AlarmRecord> alarmRecordIPage = alarmRecordMapper.selectPage(page, lambdaQueryWrapper);
        return Result.success(alarmRecordIPage);
    }

    @Override
    public Result selectAllByPageAndJhAndCategory(IPage<AlarmRecord> page, int department, int sort, String jh, String category) {
        LambdaQueryWrapper<AlarmRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LinkedList<String> yms = new LinkedList<>();
        if (department == 1) {
            yms.add("大庆");
            yms.add("川渝");
        }
        if (department == 2) {
            yms.add("川渝");
        }
        if (department == 3) {
            yms.add("大庆");
        }
        List<QjVo> list = null;
        if (yms.size() > 0)
//            findbhByYm
            list = qjService.findbhByYm(yms);
        else
            return Result.success(null);
        if (list != null && list.size() > 0) {
            List<String> ywbhs = QjVo.getYwbhs(list);
            List<String> zwbhs = QjVo.getZwbhs(list);
            lambdaQueryWrapper.and(l -> l.in(AlarmRecord::getJh, ywbhs).or().in(AlarmRecord::getJh, zwbhs));
        }
        if (jh != null && !jh.equals(""))
            lambdaQueryWrapper.eq(AlarmRecord::getJh, jh);

        if (category != null && !category.equals(""))
            lambdaQueryWrapper.eq(AlarmRecord::getAlarm_category, category);
        if (sort == 1)
            lambdaQueryWrapper.orderByAsc(AlarmRecord::getTime);
        else
            lambdaQueryWrapper.orderByDesc(AlarmRecord::getTime);
        IPage<AlarmRecord> alarmRecordIPage = alarmRecordMapper.selectPage(page, lambdaQueryWrapper);
        return Result.success(alarmRecordIPage);
    }

    @Override
    public Result selectAllByPageAndJhAndCategoryAndTime(IPage<AlarmRecord> page, int department, int sort, String jh, String category, String start, String end) {
        LambdaQueryWrapper<AlarmRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LinkedList<String> yms = new LinkedList<>();
        if (department == 1) {
            yms.add("大庆");
            yms.add("川渝");
        }
        if (department == 2) {
            yms.add("川渝");
        }
        if (department == 3) {
            yms.add("大庆");
        }
        List<QjVo> list = null;
        if (yms.size() > 0)
//            findbhByYm
            list = qjService.findbhByYm(yms);
        else
            return Result.success(null);
        if (list != null && list.size() > 0) {
            List<String> ywbhs = QjVo.getYwbhs(list);
            List<String> zwbhs = QjVo.getZwbhs(list);
            lambdaQueryWrapper.and(l -> l.in(AlarmRecord::getJh, ywbhs).or().in(AlarmRecord::getJh, zwbhs));
        }
        if (jh != null && !jh.equals(""))
            lambdaQueryWrapper.eq(AlarmRecord::getJh, jh);

        if (category != null && !category.equals(""))
            lambdaQueryWrapper.like(AlarmRecord::getAlarm_category, category);
        if (sort == 1)
            lambdaQueryWrapper.orderByAsc(AlarmRecord::getTime);
        else
            lambdaQueryWrapper.orderByDesc(AlarmRecord::getTime);

        if (Strings.isNotBlank(end))
            lambdaQueryWrapper.le(AlarmRecord::getTime, end);
        if (Strings.isNotBlank(start))
            lambdaQueryWrapper.ge(AlarmRecord::getTime, start);
        IPage<AlarmRecord> alarmRecordIPage = alarmRecordMapper.selectPage(page, lambdaQueryWrapper);
        return Result.success(alarmRecordIPage);
    }

    @Override
    public Result updateOne(AlarmRecord alarmRecord) {
        alarmRecordMapper.updateById(alarmRecord);
        return Result.success(null);
    }
}
